package com.aixin.csopsupplier.service;

import com.aixin.csopsupplier.domain.ServiceDef;
import com.aixin.csopsupplier.mapper.CsopServiceMapper;
import com.aixin.csopsupplier.model.ServiceDefModel;
import com.aixin.csopsupplier.model.SystemReturn;
import com.aixin.csopsupplier.repository.ServiceDefRepository;
import com.aixin.csopsupplier.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class ServiceDefService {

    @Autowired
    private ServiceDefRepository serviceDefRepository;
    @Autowired
    private CsopServiceMapper csopServiceMapper;


    
    public List<ServiceDef> findAll(String ServiceName) {
        if ("".equals(ServiceName)){
            ServiceName=null;
        }
        return   serviceDefRepository.getServiceByName(ServiceName);
    }

    
    public List<ServiceDef> findBySupplierID(String supplierID) {
        return serviceDefRepository.findAllBySupplierIDAndServiceStatusLessThan(supplierID, 2);
    }

    public List<ServiceDef> findSpecServices(String supplierID, String equityID, String equityLevelID) {
        return serviceDefRepository.findSpecService(supplierID, equityID, equityLevelID);
    }

    
    public List<ServiceDef> getCustomerServices(String customerID) {
        return serviceDefRepository.getCustomerServices(customerID);
    }

    
    public SystemReturn save(ServiceDefModel serviceDefModel) {

        ServiceDef serviceDef=changeServiceDef(serviceDefModel);
        serviceDef.setServiceID(Util.getUUID());
        serviceDef.setCreateTime(Util.getNowDate());
        serviceDef= serviceDefRepository.save(serviceDef);
        if (serviceDef!=null){
            return SystemReturn.getSystemTrueReturn();
        }
            return SystemReturn.getSystemFalseReturn();

    }

    
    @Transactional
    public SystemReturn update(ServiceDefModel serviceDefModel) {
        ServiceDef serviceDef= changeServiceDef(serviceDefModel);
        serviceDef.setUpdateTime(Util.getNowDate());
        csopServiceMapper.updateServiceDef(serviceDef);
         return SystemReturn.getSystemTrueReturn();
    }

    
    @Transactional
    public Page<ServiceDef> findAll(ServiceDefModel serviceDefModel, int pageNum, int pageSize) {
        if("".equals(serviceDefModel.getServiceName())){serviceDefModel.setServiceName(null);}
        if("".equals(serviceDefModel.getSupplierName())){serviceDefModel.setSupplierName(null);}

        Pageable pageRequest = new PageRequest(pageNum-1,pageSize, Sort.Direction.DESC,"createTime");
        Page<ServiceDef> pageList= serviceDefRepository.getServiceDef(serviceDefModel.getSupplierName(),serviceDefModel.getServiceName(),pageRequest);

        return pageList;
    }

    
    public SystemReturn excelImport(List<ServiceDef> list) {
        serviceDefRepository.saveAll(list);
        return SystemReturn.getSystemTrueReturn();
    }


    public ServiceDef changeServiceDef(ServiceDefModel serviceDefModel){
        ServiceDef serviceDef=new ServiceDef();
        serviceDef.setServiceName(serviceDefModel.getServiceName());
        serviceDef.setServiceChannel(serviceDefModel.getServiceChannel());
        serviceDef.setServiceIntr(serviceDefModel.getServiceIntr());
        serviceDef.setServiceStatus(serviceDefModel.getServiceStatus());
        serviceDef.setServiceProcess(serviceDefModel.getServiceProcess());
        serviceDef.setSupplierID(serviceDefModel.getSupplierID());
        serviceDef.setSupplierName(serviceDefModel.getSupplierName());
        serviceDef.setServiceID(serviceDefModel.getServiceID());
        serviceDef.setTermOfValidity(serviceDefModel.getTermOfValidity());
        return serviceDef;
    }
}
